{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f45400e",
   "metadata": {},
   "source": [
    "## 1.Python使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0fc145e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eab31be3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "# 6.请求参数\n",
    "r = requests.post(base_url,payload)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5bf29eea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "004e5007",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1647864117,bbfb544d-3ca4-40d9-8ae9-bcfbb64f326a\",\"time_used\":382,\"faces\":[{\"face_token\":\"e4e2ac66d2eea3af9c84f03dfa0b82d3\",\"face_rectangle\":{\"top\":108,\"left\":141,\"width\":107,\"height\":107}}],\"image_id\":\"JoYdaZ0h1hGb4ih1d2EZVw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7bab6bf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647864117,bbfb544d-3ca4-40d9-8ae9-bcfbb64f326a',\n",
       " 'time_used': 382,\n",
       " 'faces': [{'face_token': 'e4e2ac66d2eea3af9c84f03dfa0b82d3',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839632f8",
   "metadata": {},
   "source": [
    "## 2.人脸分析api接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3915c80c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "analyze_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\": 'e4e2ac66d2eea3af9c84f03dfa0b82d3',    \n",
    "    \"return_attributes\":'gender,age,smiling'\n",
    "}\n",
    "# 6.请求参数\n",
    "r = requests.post(analyze_url,payload)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3c0275ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a1088cc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"time_used\": 200, \"request_id\": \"1647865996,9055511c-8294-411e-83d5-2adf33aa2d04\", \"faces\": [{\"attributes\": {\"gender\": {\"value\": \"Male\"}, \"age\": {\"value\": 53}, \"smile\": {\"threshold\": 50.0, \"value\": 100.0}}, \"face_rectangle\": {\"width\": 107, \"top\": 108, \"left\": 141, \"height\": 107}, \"face_token\": \"e4e2ac66d2eea3af9c84f03dfa0b82d3\"}]}'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d6362fb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 200,\n",
       " 'request_id': '1647865996,9055511c-8294-411e-83d5-2adf33aa2d04',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': 'e4e2ac66d2eea3af9c84f03dfa0b82d3'}]}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "d168caed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3cc9303e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>e4e2ac66d2eea3af9c84f03dfa0b82d3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                           0\n",
       "face_token                  e4e2ac66d2eea3af9c84f03dfa0b82d3\n",
       "attributes.gender.value                                 Male\n",
       "attributes.age.value                                      53\n",
       "attributes.smile.threshold                              50.0\n",
       "attributes.smile.value                                 100.0\n",
       "face_rectangle.width                                     107\n",
       "face_rectangle.top                                       108\n",
       "face_rectangle.left                                      141\n",
       "face_rectangle.height                                    107"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72e15fa0",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "3a2bd9eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "# 6.请求参数\n",
    "r = requests.post(compare_url,payload)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "2cb32125",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a4704cb071af8c56fbf516c66db0f101'"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "d033b0fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "  'image_url':'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "}\n",
    "\n",
    "# 6.请求参数\n",
    "r = requests.post(compare_url,payload)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "ccd74b40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0851c02d40cdafb382265be910d98228'"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "2ea52ed0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_token1\": face_token01,\n",
    "    \"face_token2\": face_token02\n",
    "    \n",
    "}\n",
    "# 6.请求参数\n",
    "r = requests.post(compare_url,payload)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "4ba1c3f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "d399b6df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "671a7cba",
   "metadata": {},
   "source": [
    "##  封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "ef8a57c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "69e84806",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "c3ac673b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868138,0627ab00-a159-4785-a12a-8c6edc85d05c',\n",
       " 'time_used': 597,\n",
       " 'faces': [{'face_token': '545755a9624677a8427117656d815173',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "9b2790ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868150,a1885b1c-fd3f-4451-a582-5b9cd448efe2',\n",
       " 'time_used': 549,\n",
       " 'faces': [{'face_token': '3c2eb68b1f7a21ed1d6273de60129d93',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "482ea69f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
